<template>
  <LoginHeader>联合登录</LoginHeader>
  <section class="container"
           v-if="isBind">
    <div class="unbind">
      <div class="loading"></div>
    </div>
  </section>
  <section v-else
           class="container">
    <nav class="tab">
      <a @click="hasAccount = true"
         :class="{ active: hasAccount }"
         href="javascript:;">
        <i class="iconfont icon-bind" />
        <span>已有小兔鲜账号，请绑定手机</span>
      </a>
      <a @click="hasAccount = false"
         :class="{ active: !hasAccount }"
         href="javascript:;">
        <i class="iconfont icon-edit" />
        <span>没有小兔鲜账号，请完善资料</span>
      </a>
    </nav>
    <div class="tab-content"
         v-if="hasAccount">
      <CallbackBind :nickname="nickname"
                    :avatar="avatar"
                    :unionId="unionId" />
    </div>
    <div class="tab-content"
         v-else>
      <CallbackPatch :unionId="unionId" />
    </div>
  </section>
  <LoginFooter />
</template>

<script>
import { ref } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import QC from 'qc'
import LoginHeader from './components/login-header'
import LoginFooter from './components/login-footer'
import CallbackBind from './components/callback-bind'
import CallbackPatch from './components/callback-patch'
import { userQQLogin } from '@/apis/user'
export default {
  name: 'LoginCallback',
  components: { LoginHeader, LoginFooter, CallbackPatch, CallbackBind },
  setup () {
    const hasAccount = ref(false)
    const store = useStore()
    const router = useRouter()
    // 显示加载界面
    const isBind = ref(true)
    const unionId = ref(null)
    // 如果qq登录成功
    if (QC.Login.check()) {
      // 调用接口,根据qq的id去查询此qq是否绑定账号
      QC.Login.getMe((openId) => {
        unionId.value = openId
        userQQLogin(openId)
          .then((res) => {
            // 登陆成功后 保存用户信息，根据vuex的redirectpath地址回跳
            const { id, account, avatar, mobile, nickname, token } = res.result
            // console.log(res)
            store.commit('user/setUser', {
              id,
              account,
              avatar,
              mobile,
              nickname,
              token
            })
            const rpath = store.state.user.redirectpath
            router.push(rpath)
            store.dispatch('cart/mergeCart').then(() => {
              const rpath = store.state.user.redirectpath
              router.push(rpath)
            })
          })
          .catch((e) => {
            // 没有绑定
            isBind.value = false
          })
      })
    }
    return { hasAccount, isBind, unionId }
  }
}
</script>

<style scoped lang="less">
.container {
  padding: 25px 0;
  position: relative;
  height: 730px;
  .unbind {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    padding: 25px 0;
    z-index: 99;
    .loading {
      height: 100%;
      background: #fff url(../../assets/images/load.gif) no-repeat center /
        100px 100px;
    }
  }
}
.tab {
  background: #fff;
  height: 80px;
  padding-top: 40px;
  font-size: 18px;
  text-align: center;
  a {
    color: #666;
    display: inline-block;
    width: 350px;
    line-height: 40px;
    border-bottom: 2px solid #e4e4e4;
    i {
      font-size: 22px;
      vertical-align: middle;
    }
    span {
      vertical-align: middle;
      margin-left: 4px;
    }
    &.active {
      color: @xtxColor;
      border-color: @xtxColor;
    }
  }
}
.tab-content {
  min-height: 600px;
  background: #fff;
}
</style>
